package com.snow.rbac.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * All rights Reserved, Designed By www.cusc.com
 *
 * @author yanke
 * @Title: MybatisPlusConfig
 * @Package com.snow.rbac.config
 * @Description:类描述
 * @Email yanke@cu-sc.com
 * @version: v1.0.0
 * @date: 2021/6/16/0016 17:09
 * @Copyright: 2021  www.faw.com.cn Inc. All rights reserved. <br/>
 * Modification History:<br/>
 * Date         Author          Version            Description<br/>
 * *---------------------------------------------------------*<br/>
 * 2021/6/16/0016     Administrator           v1.0.0               修改原因<br/>
 * @since JDK 1.8
 */
@Configuration
@MapperScan(basePackages = {"com.snow.rbac.mapper"})
public class MybatisPlusConfig {

    private String[] tableName = {"user_info","role"};

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
        HashMap<String, TableNameHandler> map = new HashMap<String, TableNameHandler>(2);
        for (String tn : tableName) {
            map.put(tn, (sql, tableName) -> {
                return "dayou." + tn;
            });
        }
//        HashMap<String, TableNameHandler> map = new HashMap<String, TableNameHandler>(2) {{
//            put("user_info", (sql, tableName) -> {
//                return "dayou." + tableName;
//            });
//        }};
        dynamicTableNameInnerInterceptor.setTableNameHandlerMap(map);
        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
        return interceptor;
    }
}
